<?php

namespace app\common\business;

use \think\Request;
use \think\Session;
use app\common\mysql\Users as usersMysql;
use app\common\business\user\Point as pointBn;
use app\common\business\User as userBn;
class Integral extends \app\common\business\AbstractModel {

    /**
     * 会员列表
     * page
     * count
     * need_total
     */
    public static function integrallist($params) {
        self::_checkPageAndCount($params);
        $where = self::_getWhere($params);
        $listdata = usersMysql::name('integral_goods')
                ->where($where)
                ->limit($params['offset'], $params['count'])
                ->select();
        if (!empty($listdata)) {
            $listdata = self::_adapterList($listdata);
        }
        if (isset($params["need_total"]) && $params["need_total"] == 1) {
            return array("listdata" => $listdata, "total" => self::total($params));
        }
        return array("listdata" => $listdata);
    }

    /**
     * 获取Query条件
     *
     * @param array $params
     * @return array
     */
    protected static function _getQuery($params) {
        $query = array();
        if (isset($params["type"])) {
            $query['type'] = $params["type"];
        }
        if (isset($params["user_id"])) { //userid
            $query['user_id'] = $params["user_id"];
        }

        if (isset($params["guess_first"])) { //冠军
            $query['guess_first'] = $params["guess_first"];
        }

        if (isset($params["str_time"])) { //时间
            $query['str_time'] = $params["str_time"];
        }
        if (isset($params["end_time"])) { //时间
            $query['end_time'] = $params["end_time"];
        }

        return $query;
    }

    /**
     * 适配输出
     *
     * @params
     * @return array
     */
    protected static function _adapterList($result) {

        foreach ($result as $k => $val) {

        }
        return $result;
    }

    /**
     * 获取where条件
     *
     * @param array $params
     * @return array
     */
    protected static function _getWhere($params) {
        $where = array();
        if (isset($params["type"]) && !empty($params['type'])) { // 
            $where['type'] = $params["type"];
        }
        if (isset($params["user_id"]) && !empty($params['user_id'])) { // 
            $where['user_id'] = $params["user_id"];
        }

        if (isset($params["guess_first"]) && !empty($params['guess_first'])) { // 
            $where['guess_first'] = $params["guess_first"];
        }

        if (isset($params["str_time"]) && !empty($params['end_time'])) { // 
            if (isset($params['str_time'])) {
                $params['str_time'] = strtotime($params['str_time']);
            }
            if (isset($params['end_time'])) {
                $params['end_time'] = strtotime($params['end_time']);
            }
            $where['add_time'] = array('between', [$params['str_time'], $params['end_time']]);
        }
        return $where;
    }

    /**
     * 计算总数
     *
     * @param array $params
     * @return int
     */
    public static function total($params) {
        $where = self::_getWhere($params);
        $count = usersMysql::name('guess')
                ->where($where)
                ->count();
        return $count;
    }

    /**
     * 是记录记录
     * user_id 当前用户
     * guess_16
     */
    public static function exchangegoods($post) {

        $userinfo=userBn::userInfo($post['user_id']);
        if(empty($userinfo['mobile']))
         {
                return exception('请绑定手机号码', 10000003);
         }
        //用户ID是否存在
        $data = usersMysql::name('integral_goods')
                ->where(array('id'=>$post['integ_id'],'status'=>1))
                ->find();
        if (empty($data)) {
             return exception('该商品不存在', 10000003);
        }
        if($data['limit_num']<=0){
            return exception('此商品已没有了', 10000003);
        }
        if($data['user_limit']>0){
            $where = array('user_id' => $post['user_id'],'integ_id'=>$post['integ_id']);
            $exchange = usersMysql::name('integral_exchange')
                ->where($where)
                ->column('id');
            if(count($exchange)>=$data['user_limit']){
                return exception('此商品每个用户只能兑换'.$data['user_limit'].'次', 10000003);
            }
        }
        $insert = $post;
        $insert['add_time'] = time();
        usersMysql::startTrans();
        $r = usersMysql::name('integral_exchange')->insertGetId($insert);
        if($r){

            $point['user_id']=$post['user_id'];
            $point['point']  =$data['point'];
            $point['source_type']=0;
            $point['change_type']=2;
            $point['point_type'] =6;
            $point['change_memo']=' 使用'.$data['point'].'积分获得'.$data['goods'];
            $r2=pointBn::updatePoint($point);
            if($r2){
                usersMysql::commit();
                return true;
            }
        }
            usersMysql::rollback();
            return false;

    }

    /**
     * 用户兑换积分列表
     * page
     * count
     * need_total
     */
    public static function usergoods($params = array()) {

        self::_checkPageAndCount($params);
        $listdata = usersMysql::name('integral_exchange')
                ->alias('i')
                ->join('clt_integral_goods s','s.id=i.integ_id','left')
                ->where('user_id='.$params['user_id'])
                ->select();

        $total =usersMysql::name('integral_exchange')
            ->alias('i')
            ->join('clt_integral_goods s','s.id=i.integ_id','left')
            ->where('user_id='.$params['user_id'])
            ->count();


        return array("listdata" => $listdata, 'total' => $total);
    }
    /**
     * 观看状态
     *
     * */
    public static function save($post,$id) {
      $post['end_time']=strtotime($post['end_time']);
        if($id){
            usersMysql::name('integral_goods')
                ->where(array('id'=>$id))
                ->update($post);
        }else{
            $post['add_time']=time();
            usersMysql::name('integral_goods')
                ->insert($post);
        }

        return true;
    }


}
